import { clear2FAToken, clearToken, setToken } from '@/util/auth'
import { defaultState } from '@/store/module/user/index'
import { patch } from '@/store/module'
import Vue from 'vue'

// function getPath(path) {
//   if (path.indexOf('?') > -1) {
//     return path.substring(0, path.indexOf('?'))
//   }
//   return path
// }

export default {
  setToken(state, { token, auto }) {
    state.token = token
    setToken(token, auto)
  },
  // updateMenuPath(state, path) {
  //   const temp = getPath(path)
  //   const children = findTreeChildren(state.menu, item => item.urlPath === temp)
  //   children.forEach(value => {
  //     value.urlPath = path
  //   })
  // },
  notEmpty(state) {
    state.empty = false
  },
  invalidToken(state) {
    state.tokenValid = false
  },
  validToken(state) {
    state.tokenValid = true
  },
  addAuthority(state, authority) {
    Vue.set(state.permissions, authority, true)
    state.permissions[authority] = true
  },
  removeAuthority(state, authority) {
    Vue.delete(state.permissions, authority)
  },
  patch,
  reset(state) {
    clear2FAToken()
    clearToken()
    patch(state, defaultState)
  }
}
